/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/
package plar.core;

/**
 * the possible keys that can be used by a player, this class should contain the keys actually pressed by the user,
 * you can press more than one key at the same time.
 *
 * @author Antonio Ragagnin
 *
 */
public class KeyFlag {
    public static final short LEFT = 0x0001;
    public static final short RIGHT = 0x0002;
    public static final short UP = 0x0004;
    public static final short DOWN = 0x0008;
    public static final short JUMP = 0x0010;
    public static final short FIRE = 0x0020;
    public static final short SPECIAL = 0x0040;
    public static final short k1 = 0x0080;
    public static final short k2 = 0x0100;
    public static final short k3 = 0x0200;
    public static final short k4 = 0x0400;
    public static final short k5 = 0x0800;
    public static final short NONE = 0x0000;

    public short Key=0x00;
    /*
     * add
     * Key b -> Key
     * 0   0    0
     * 0   1    1
     * 1   0    1
     * 1   1    1
     *
     *
     * del
     * Key b -> Key
     * 0   0    0
     * 0   1    0
     * 1   0    1
     * 1   1    0
     */
	 
	 /**
	 * initialize the key
	 */
    public KeyFlag(short s) {
        Key=s;
    }
    public KeyFlag() {}
	
	/**
	* add a pressed key.
	*/
    public void add(KeyFlag b) {
        Key =(short) (Key | b.Key);
    }
	
	/**
	* remove a pressed key
	*/
    public void del(KeyFlag b) {
        Key =(short) (Key & ~b.Key);
    }
	
	/**
	* chechk if a key is present in the pressed keys.
	*/
    public boolean check(KeyFlag b) {
        return ((Key & b.Key)==b.Key);
    }
	
	/**
	* add a pressed key.
	*/
    public void add(short b) {
        Key =(short) (Key | b);
    }
	
	/**
	* remove a pressed key
	*/
    public void del(short b) {
        Key =(short) (Key & ~b);
    }
	
	/**
	* chechk if a key is present in the pressed keys.
	*/
    public boolean check(short b) {
        return ((Key & b)==b);
    }
	
	/**
	* return the number of one of the pressed numbers.
	*/
    public int number()
    {
        if(check(k1)) return 1;
        else if(check(k2)) return 2;
        else if(check(k3)) return 3;
        else if(check(k4)) return 4;
        else if(check(k5)) return 5;
        else return 0;
    }
    public boolean equals(Object arg0)
    {
        KeyFlag kf = (KeyFlag) arg0;
        if(kf!=null)	return (kf.Key==Key);
        else return false;
    }
    public String toString() {
        return Short.toString(Key);
    }

}
